package com.yunhe.sms.dao.system.impl;

import com.yunhe.commens.dao.impl.BaseDaoImpl;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.commens.util.JdbcHelp;
import com.yunhe.sms.dao.system.PremissionDao;
import com.yunhe.sms.entity.system.Premission;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/19 0019 10:52
 * @Description: 对数据库的操作
 */
public class PremissionDaoImpl extends BaseDaoImpl<Premission> implements PremissionDao {

    @Override
    public void save(Premission premission) {
        String sql = "insert into sys_premission(name,url,type,parent_id,button,creat_time,sort_no ) values(?,?,?,?,?,now(),? )";
        super.cut ( sql,premission.getName (),premission.getUrl (),premission.getType (),premission.getParentsId (),premission.getButtonId (),premission.getSortNo () );
    }

    @Override
    public void deleted(Long id) {
        String sql="update sys_premission set deleted=1 where id=?";
        super.cut ( sql,id );
    }



    @Override
    public void update(Premission premission) {
        String sql = "update sys_premission set name=?,url=?,button=?,type=?,parent_id=?,sort_no=? where deleted=0 and id=?";
        super.cut ( sql,premission.getName (),premission.getUrl (),premission.getButtonId (),premission.getType (),premission.getParentsId (),premission.getSortNo (),premission.getId () );
    }

    @Override
    public Premission find(Long id) {
        String sql="select id,name,url,type,button buttonId,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and id=?";
        Premission premission = super.quaryOne ( sql, id );
        return premission;
    }

    @Override
    public List<Long> findRoll(Long rolId) {
        String sql="select sp.id from sys_premission sp, sys_role_premi rp,sys_role r where sp.id=rp.premi_id and r.id=rp.role_id and sp.deleted=0 and r.id=?";
        QueryRunner queryRunner = new QueryRunner ( JdbcHelp.getDataSource () );
        List<Long> query = null;
        try {
            query = queryRunner.query ( sql, new ColumnListHandler<Long> (), rolId );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return query;
    }

    @Override
    public List<Premission> findrollpremi(Long rolId) {
        String sql="select sp.id,sp.name,sp.url,sp.button buttonId,sp.parent_id parentsId,sp.type from sys_premission sp, sys_role_premi rp,sys_role r where sp.id=rp.premi_id and r.id=rp.role_id and sp.deleted=0 and r.id=? ";
        return super.quaryList ( sql,rolId );
    }

    @Override
    public List<Premission> findPremi(Long rolId, int type) {
        String sql="select sp.id,sp.name,sp.url,sp.button buttonId,sp.parent_id parentsId,sp.type from sys_premission sp, sys_role_premi rp,sys_role r where sp.id=rp.premi_id and r.id=rp.role_id and sp.deleted=0 and r.id=? and type=? ";
        return super.quaryList ( sql, rolId, type );
    }

    @Override
    public List<Premission> findList() {
        String sql="select id,name,url,button buttonId,type,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0";
        List<Premission> premissions = super.quaryList ( sql );
        return premissions;
    }

    @Override
    public Pagedao<Premission> page(int offset,int size) {
        String listsql="select id,name,url,button buttonId,type,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and type in (1,9) ";
        String countsql="select count(*) from sys_premission where deleted=0 and type in (1,9) ";
        Pagedao<Premission> quaryPage = super.quaryPage ( listsql, countsql, offset, size );
        return quaryPage;
    }

    @Override
    public List<Premission> findType(int type) {
        String sql="select id,name,url,type,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and type=?";
        List<Premission> premissions = super.quaryList ( sql,type );
        return premissions;
    }

    @Override
    public List<Premission> findParent(int parentId) {
        String sql="select id,name,url,type,button buttonId,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and parent_id=?";
        List<Premission> premissions = super.quaryList ( sql,parentId );
        return premissions;
    }

    @Override
    public List<Premission> findTypeList(String str) {
        String sql="select name,url,button buttonId,type,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and "+str;
        List<Premission> premissions = super.quaryList ( sql );
        return premissions;
    }

    @Override
    public List<Premission> findButton(Long parent_id) {
        String sql="select name,url,button buttonId,type,parent_id parentsId,sort_no sortNo from sys_premission where deleted=0 and type=3 and parent_id=? ";
        List<Premission> premissions = super.quaryList ( sql,parent_id );
        return premissions;
    }
}
